package com.ruoyi.system.controller;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.http.FormDataFilePart;
import com.huaweicloud.sdk.frs.v2.FrsClient;
import com.huaweicloud.sdk.frs.v2.model.*;
import com.huaweicloud.sdk.frs.v2.region.FrsRegion;
import com.ruoyi.common.core.domain.AjaxResult;
import io.vertx.core.buffer.Buffer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/system/compareFace")
public class CompareFaceController {

    private static FrsClient client;

    static {

        ICredential auth = new BasicCredentials()
                .withAk("9LCWQGUFXTMAVTQQHXPU")
                .withSk("ixf7b2vxl7y6WA7Lwa7l9wOq8009ZJUtFMSrc2K8");

        client = FrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(FrsRegion.valueOf("cn-north-4"))
                .build();
    }


    @PostMapping("/compare")
    public AjaxResult compareFaceByFiles(MultipartFile[] files) throws Exception {

        CompareFaceByFileRequest request = new CompareFaceByFileRequest();
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        CompareFaceByFileRequestBody body = new CompareFaceByFileRequestBody();

        body.setImage1File(convert(files[0]));
        body.setImage2File((convert(files[1])));
        request.setBody(body);
        CompareFaceByFileResponse response = null;
        try {
            response = client.compareFaceByFile(request);
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 上传并返回新文件名称
        AjaxResult ajax = AjaxResult.success();
        ajax.put("msg", "上传文件成功");
        ajax.put("code", 200);
        ajax.put("data", response);
        return ajax;
    }
    public static FormDataFilePart convert(MultipartFile multipartFile) throws IOException {
        // 获取 MultipartFile 的文件名
        String fileName = multipartFile.getOriginalFilename();
        // 创建 FormDataFilePart 对象
        return new FormDataFilePart(multipartFile.getInputStream(), fileName);
    }
    /**
     * 人脸检测
     */
    @PostMapping("/detectFace")
    public AjaxResult detectFace(@RequestParam("file") MultipartFile file) throws IOException {
        DetectFaceByFileRequest request = new DetectFaceByFileRequest();
        DetectFaceByFileRequestBody body = new DetectFaceByFileRequestBody();
        body.setImageFile(convert(file));
        request.setBody(body);
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        DetectFaceByFileResponse response = new DetectFaceByFileResponse();
        try {
            response = client.detectFaceByFile(request);
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 处理人脸检测逻辑
        // 这里可以使用华为云的SDK进行人脸检测
        // 返回结果
        return AjaxResult.success(response);
    }

    /**
     * 人脸搜索
     */
    @PostMapping("/searchFace")
    public AjaxResult searchFace(@RequestParam("file") MultipartFile file,
                                 @RequestParam("faceSetName") String faceSetName) throws IOException {
        SearchFaceByFileRequest request = new SearchFaceByFileRequest();
        SearchFaceByFileRequestBody body = new SearchFaceByFileRequestBody();
        body.setImageFile(convert(file));
        request.setFaceSetName(faceSetName);
        request.setBody(body);
        request.setEnterpriseProjectId("06eaaa18a78025312f97c00abbb0c538");
        SearchFaceByFileResponse response = new SearchFaceByFileResponse();
        try {
            response = client.searchFaceByFile(request);
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 处理人脸检测逻辑
        // 这里可以使用华为云的SDK进行人脸检测
        // 返回结果
        return AjaxResult.success(response);
    }

}
